package com.question.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.question.Constant.BaseConstant;
import com.question.Vo.SessionWebUserDto;
import com.question.common.Result;
import com.question.entity.User;
import com.question.enums.ResultEnums;
import com.question.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/")
public class AccoutController {
    @Resource
    private UserService userService;
    @RequestMapping("login")
    public Result login(HttpSession httpSession, String username, String password, Integer roles){
        SessionWebUserDto sessionWebUserDto=new SessionWebUserDto();
        sessionWebUserDto=userService.login(username,password,roles);
        httpSession.setAttribute(BaseConstant.key,sessionWebUserDto);
        return Result.success(sessionWebUserDto);

    }
    @RequestMapping("/register")
    public Result register(String username,String password){
        userService.register(username,password);
        return Result.success("成功");
    }
    @RequestMapping("/logout")
    public Result logout(HttpSession httpSession){
        httpSession.removeAttribute(BaseConstant.key);
        return Result.success("成功");
    }
    @RequestMapping("/info")
    public Result info(HttpSession httpSession){
        SessionWebUserDto sessionWebUserDto= (SessionWebUserDto) httpSession.getAttribute(BaseConstant.key);
        return Result.success(sessionWebUserDto);
    }
    @RequestMapping("/user")
    public Result user(){
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getRoles,1);
        List<User> list=userService.list(lambdaQueryWrapper);
        return Result.success(list);
    }


}
